<?php
declare (strict_types=1);

namespace app\controller;

use app\model\Goal;
use app\model\Task;
use app\model\User as UserModel;
use think\facade\Request;
use think\facade\Validate;

class User
{
    public function register()
    {
        // 获取表单数据
        $data = Request::post();

        // 验证规则
        $validate = Validate::rule([
            'username' => 'require|max:50',
            'email' => 'require|email|unique:users',
            'password' => 'require|min:6',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 创建用户
        $user = new UserModel();
        $user->username = $data['username'];
        $user->email = $data['email'];
        $user->password = password_hash($data['password'], PASSWORD_DEFAULT);
        $user->save();

        return json(['message' => '用户注册成功', 'user_id' => $user->id]);
    }

    public function login()
    {
        $data = Request::post();

        // 验证输入
        $validate = Validate::rule([
            'email' => 'require|email',
            'password' => 'require|min:6',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 查找用户
        $user = UserModel::where('email', $data['email'])->find();
        if (!$user || !password_verify($data['password'], $user->password)) {
            return json(['error' => '邮箱或密码错误'], 401);
        }

        // 更新最后登录时间
        $user->last_login = date('Y-m-d H:i:s');
        $user->save();

        $token = md5($user->user_id . time() . rand(1000, 9999));
        cache($token, $user->user_id, 3600 * 24 * 7);
        // 返回成功
        return json(['message' => '登录成功', 'token' => $token]);
    }

    //目标创建
    public function createGoal()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'title' => 'require|max:255',
            'description' => 'max:1000',
            'priority' => 'in:low,medium,high',
            'start_date' => 'date',
            'end_date' => 'date',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 创建目标
        $goal = new Goal();
        $goal->user_id = $data['user_id'];  // 应从登录状态中获取
        $goal->title = $data['title'];
        $goal->description = $data['description'];
        $goal->priority = $data['priority'] ?? 'medium';
        $goal->start_date = $data['start_date'];
        $goal->end_date = $data['end_date'];
        $goal->save();

        return json(['message' => '目标创建成功', 'goal_id' => $goal->goal_id]);
    }

    //任务分配
    public function assignTask()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'goal_id' => 'require|integer',
            'title' => 'require|max:255',
            'description' => 'max:1000',
            'priority' => 'in:low,medium,high',
            'due_date' => 'date',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 分配任务
        $task = new Task();
        $task->goal_id = $data['goal_id'];
        $task->title = $data['title'];
        $task->description = $data['description'];
        $task->priority = $data['priority'] ?? 'medium';
        $task->due_date = $data['due_date'];
        $task->save();

        return json(['message' => '任务分配成功', 'task_id' => $task->task_id]);
    }

    // 更新用户资料
    public function update()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'username' => 'require|max:50',
            'email' => 'require|email|max:100',
            'avatar' => 'max:255',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $user = UserModel::find($data['user_id']);
        if (!$user) {
            return json(['status' => 'error', 'message' => 'User not found']);
        }

        $user->username = $data['username'];
        $user->email = $data['email'];
        $user->avatar = $data['avatar'] ?? $user->avatar;
        $user->save();

        return json(['status' => 'success', 'message' => 'Profile updated successfully']);
    }

    // 修改用户密码
    public function changePassword()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'user_id' => 'require|integer',
            'current_password' => 'require',
            'new_password' => 'require|min:6',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $user = UserModel::find($data['user_id']);
        if (!$user || !password_verify($data['current_password'], $user->password)) {
            return json(['status' => 'error', 'message' => 'Current password is incorrect']);
        }

        $user->password = password_hash($data['new_password'], PASSWORD_BCRYPT);
        $user->save();

        return json(['status' => 'success', 'message' => 'Password changed successfully']);
    }


}
